package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.SeniorExpertListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.SeniorExpert;
import com.isunland.managesystem.entity.SeniorExpertOriginal;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/24 15:47
 */
public class ExpertChooseListFragment extends BaseListFragment {

  protected static final String EXTAR_VALUE =
      "com.isunland.managesystem.ui.ExpertChooseListFragment.EXTAR_VALUE";

  private ArrayList<SeniorExpert> mList;
  private String kindCode;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.expertList);
    kindCode = getArguments().getString(EXTAR_VALUE);
    mList = new ArrayList<>();
  }

  @Override public String getUrl() {
    return ApiConst.URL_EXPERT;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("beginregDate", "");
    params.put("endregDate", "");
    params.put("exportName", "");
    params.put("exportKindCode", kindCode);
    params.put("orderField", "order_no");
    params.put("orderSeq", "desc");
    params.put("dataStatus", DataStatus.PUBLIS);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    SeniorExpertOriginal original = new Gson().fromJson(json, SeniorExpertOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      SeniorExpertListAdapter mAdapter = new SeniorExpertListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  //可以查看专家发布的帖子和回复
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    SeniorExpert content = mList.get(position - 1);
    Intent intent = new Intent();
    intent.putExtra(EXTAR_VALUE, content);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
    super.onListItemClick(l, v, position, id);
  }

  public static ExpertChooseListFragment newInstance(String kindCode) {
    Bundle args = new Bundle();
    args.putString(EXTAR_VALUE, kindCode);
    ExpertChooseListFragment fragment = new ExpertChooseListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
